package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.Result;
import com.itheima.entity.User;
import com.itheima.entity.Answer;
import com.itheima.entity.UResult;
import com.itheima.mapper.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/result")
public class ResultController {

    @Autowired
    private ResultMapper resultMapper;

    @RequestMapping("selectStatus")
    public Result selectStatus(Integer userId, Integer courseId) {
        try {
            boolean status = true;
            // 构建条件
            LambdaQueryWrapper<UResult> Wrapper = new LambdaQueryWrapper<>();
            Wrapper.eq(UResult::getUserId, userId)
                    .eq(UResult::getCourseId, courseId);
            // 看看有没有
            List<UResult> resultList = resultMapper.selectList(Wrapper);
            // 如果没有，就返回false
            if (resultList.size() == 0) status = false;
            return new Result(true, "查询成功", status);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @PostMapping("selectScope")
    public Result selectScope(Integer userId, Integer courseId) {
        try {
            Integer score = 0;
            // 构建条件
            LambdaQueryWrapper<UResult> Wrapper = new LambdaQueryWrapper<>();
            Wrapper.eq(UResult::getUserId, userId)
                    .eq(UResult::getCourseId, courseId);
            // 看看有没有
            List<UResult> resultList = resultMapper.selectList(Wrapper);
            // 如果有，就返回score
            if (resultList.size() != 0) {
                score = resultList.get(0).getScore();
                return new Result(true, "查询成功", score);
            } else return new Result(false, "查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

}
